//
//  PCGamePayController.m
//  PhotoCircle
//
//  Created by GL on 15/12/20.
//  Copyright © 2015年 wujungao. All rights reserved.
//

#import "PCGamePayController.h"
#import "PCUserGameModel.h"
#import "PCMeStore.h"
#import <AlipaySDK/AlipaySDK.h>
#import "PCUserModel.h"
#import "GMTipsHelper.h"

@interface PCGamePayController ()

@property (weak, nonatomic) IBOutlet UILabel *totalFeeLabel;
@property (nonatomic, strong) PCUserModel *userModel;
@property (nonatomic, strong) PCUserGameModel *gModele;

@end

@implementation PCGamePayController

-(PCUserModel *)userModel{
    _userModel = [[PCUserInfoManager sharedUserInfo] readUserInfo];
    return _userModel;
}

-(void)configUI
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(aliPayCallBack:)
                                                 name:ALIPAY_NOTIFICATION_NAME
                                               object:nil];

    self.gModele = [PCMeStore sharedMeStore].mGameModel;
    _totalFeeLabel.text = [NSString stringWithFormat:@"参赛费：%@元",self.gModele.gTotalFee];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configUI];
    // Do any additional setup after loading the view.
}

- (IBAction)backEvent:(UIButton *)sender {
    
    [GMTipsHelper showDismiss];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ALIPAY_NOTIFICATION_NAME object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)payEvent:(UIButton *)sender {
    // 支付
    NSDictionary *pDic = @{@"item_type":@"GamePhoto",@"item_uuid":self.gModele.gUUID,@"token":self.userModel.userToken};
    [GMTipsHelper showLoading];
    [[PCMeStore sharedMeStore] requestAliPay:pDic successHandler:^(NSInteger responCode,NSString *payString){
        
        if (responCode == API_RESPONSE_OK) {
            [GMTipsHelper showDismiss];
            //    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
            NSString *appScheme = @"alipaysdk";
            [[AlipaySDK defaultService] payOrder:payString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
                
                NSLog(@"resultDic");
                if ([[resultDic objectForKey:@"resultStatus"] integerValue] == 9000) {
                    // 支付成功
                    [GMTipsHelper showSuccess:@"支付成功"];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshGameList" object:nil];
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshGameDetail" object:nil];

                    [self performSelector:@selector(popViewController) withObject:nil afterDelay:0.8];;

                }else{
                    // 支付失败
                    [GMTipsHelper showFail:@"支付失败"];
                }
            }];
        }else{
            [GMTipsHelper showFail:@"请求流水号失败"];
        }
    } errorHandler:^{
        [GMTipsHelper showBadNetwork];
    }];
}

-(void)aliPayCallBack:(NSNotification *)notification
{
    NSDictionary *resultDic = notification.object;
    NSLog(@"resultDic");
    if ([[resultDic objectForKey:@"resultStatus"] integerValue] == 9000) {
        // 支付成功
        [GMTipsHelper showSuccess:@"支付成功"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshGameList" object:nil];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshGameDetail" object:nil];
        
        [self performSelector:@selector(popViewController) withObject:nil afterDelay:0.8];;
        
    }else{
        // 支付失败
        [GMTipsHelper showFail:@"支付失败"];
    }

}

-(void)popViewController
{
    [self backEvent:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
